﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Net.Mail;
using System.Net;

namespace Hired
{
    public partial class notactivated : System.Web.UI.Page
    {
        private string username;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["usernameToActivate"] != null)
            {
                username = Session["usernameToActivate"].ToString();
                lblUsername.Text = "<strong>" + username + "</strong>";
            }
            else
            {
                Response.Redirect("home.aspx");
            }
        }

        protected void btnSendActivationEmail_Click(object sender, EventArgs e)
        {
            string email = "";
            object account = DBAccount.RetrieveAccountByUsername(username);
            switch (DBAccount.RetrieveAccountTypeByAccount(account))
            {
                case "CompanyAccount":
                    email = ((CompanyAccount)(account)).Email;
                    break;
                case "EmployerPassport":
                    email = ((EmployerPassport)(account)).Email;
                    break;
                case "JobseekerAccount":
                    email = ((JobseekerAccount)(account)).Email;
                    break;
            }

            string from = ConfigurationManager.AppSettings["Username"];
            string server = ConfigurationManager.AppSettings["MailServer"];
            string password = ConfigurationManager.AppSettings["password"];

            MailMessage mail = new MailMessage(from, email);
            SmtpClient smtp = new SmtpClient(server, 587);
            NetworkCredential nc = new NetworkCredential(from, password);

            mail.Subject = "[Hired] Activate your account";
            mail.IsBodyHtml = true;
            mail.Body = "You have made a request for another activation e-mail."
                + "You can activate your account @ http://hired.com/activate.aspx?username=" + username + "@activationID=" + Encryption.md5Encrypt(username)
                + "\n \n Alternatively, you can go <a href=\"http://hired.com/activate.aspx\">here</a>"
                    + "\n Your activation ID:" + Encryption.md5Encrypt(username);

            smtp.EnableSsl = true;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = nc;

            try
            {
                smtp.Send(mail);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
    }
}